package name.weily.liba.tools

import android.content.Context
import android.view.View
import android.widget.Toast


private var toast: Toast? = null

fun toastString(resId: Int, cxt: Context = ContextProvider.getAppCxt(), singleton: Boolean = false, duration: Int = Toast.LENGTH_SHORT) {
    toastString(cxt.getString(resId), cxt, singleton, duration)
}

fun toastString(string: String, cxt: Context = ContextProvider.getAppCxt(), singleton: Boolean = false, duration: Int = Toast.LENGTH_SHORT) {
    if (singleton) {
        toast?.cancel()
        toast = Toast.makeText(cxt, string, duration).apply {
            show()
        }
    } else {
        Toast.makeText(cxt, string, duration).show()
    }
}

fun toastView(cxt: Context, view: View, singleton: Boolean = false, duration: Int = Toast.LENGTH_SHORT) {
    if (singleton) {
        toast?.cancel()
        toast = Toast(cxt).apply {
            setView(view)
            setDuration(duration)
            show()
        }
    } else {
        Toast(cxt).apply {
            setView(view)
            setDuration(duration)
            show()
        }
    }
}